package com.wenx.restcamel;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultMessage;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Map;

public class AppHttp1{
    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext(); // 1. 创建 CamelContext.
        context.setTracing(true);
        context.start(); // 4. 启动CamelContext.
        context.addRoutes((new AppHttp1()).new test1());
        context.addRoutes((new AppHttp1()).new test2());
        Thread.sleep(Integer.MAX_VALUE);  // 为了保持CamelContext处于工作状态，这里需要sleep主线程
        context.stop(); // 最后停止CamelContext
    }
    public class test1 extends RouteBuilder{
        @Override
        public void configure() throws Exception {
            //JsonPathExpression jsonPathExpression = new JsonPathExpression("$.name");
            //jsonPathExpression.setResultType(String.class);
            from("quartz://report?cron=10 * * * * ?&stateful=true")
                    .to("http://localhost:8080/demo/hello4?id=123&name=abc")
                    .to("direct:directRouteB")
                    //.setHeader("type",jsonPathExpression)
                    .choice()
                        .when(header("type").isEqualTo("abc"))
                            .setBody(constant("abc"))
                        .when(header("type").isEqualTo("aaa"))
                            .process(new Processor() {
                                @Override
                                public void process(Exchange exchange) throws Exception {
                                    Map<String,Object> map = exchange.getIn().getHeaders();
                                    for(Map.Entry<String,Object> eap : map.entrySet()){
                                        System.out.println("map的值: "+eap.getKey()+"  "+eap.getValue());
                                    }
                                   exchange.getOut().setBody("aaa");
                                }
                            })
                        .otherwise()
                            .setBody(constant("123"))
                    .endChoice()
                    .end()
                    .to("file:d:/log?fileName=server1.html");
        }
    }

    public class test2 extends RouteBuilder{
        @Override
        public void configure() throws Exception {
                    from("direct:directRouteB")
                            .process(new Processor() {
                                @Override
                                public void process(Exchange exchange) throws Exception {
                                    DefaultMessage message = (DefaultMessage) exchange.getIn();
                                    InputStream bodyStream =  (InputStream)message.getBody();
                                    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                                    byte[] contextBytes = new byte[4096];
                                    int realLen;
                                    while((realLen = bodyStream.read(contextBytes , 0 ,4096)) != -1) {
                                        outStream.write(contextBytes, 0, realLen);
                                    }
                                    String inbody = new String(outStream.toByteArray(),"utf-8");
                                    System.out.println("inbody="+inbody);
                                    outStream.close();
                                    bodyStream.close();
                                    exchange.getOut().setBody(inbody+"|| 这是我自定义的内容！");
                                }
                            })
                    .to("file:d:/log?fileName=server2.html");
        }
    }
}
